package com.shankungfu.main;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shankungfu.GlobleConstant;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.SplashBean;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.TestUtils;
import com.shankungfu.util.WebService;

public class SplashScreenActivity extends Activity {

	private int splashTime = 5000;
	public DisplayImageOptions options;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				startMainActivity();
			}

		}

	};

	private ImageView ad_iv;

	private ImageLoader imageLoader;
	private ImageView splash_icon;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_layout);

		ad_iv = (ImageView) findViewById(R.id.ad_iv);
		splash_icon = (ImageView) findViewById(R.id.splash_icon);
		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).build();

		getData();
		Message msg = new Message();
		msg.what = 1;
		handler.sendMessageDelayed(msg, splashTime);

	}


	private void getData() {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "welcome_ad");
		MyAsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				if (!TextUtils.isEmpty(content)
						&& content.contains("\"result\":0")) {
					SplashBean splashBean = JSON.parseObject(content,
							SplashBean.class);
					String url = splashBean.getAdData().getAd_img_url();
					String text = splashBean.getAdData().getAd_txt();
					String serviceId = splashBean.getServiceId();

					splash_icon.setVisibility(View.GONE);
					TestUtils.logE("图片地址是： " + url);
					imageLoader.displayImage(url, ad_iv, options, null);
					
				}
			}

			@Override
			public void onFailure(String content) {
			}
		};
		responseHandler.setShowNotNetToast(false);
		httpclient.request(GlobleConstant.SPLASH_ADD, params, responseHandler);
	}

	private void startMainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
	}

}
